package operation;

import books.Book;
import books.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {

    @Override
    public void work(BookList bookList) {
        if(bookList.getBookSize() == 0){
            System.out.println("书架已经没有书了");
            return;
        }
        int tmp = search(bookList) ;
        if(tmp == -1){
            System.out.println("没有找到你要删除的图书名");
        }else {
            int len = bookList.getBookSize() - 1 ;
            if(tmp == len){
                bookList.books[tmp] = null ;
                bookList.setBookSize(len);
                return;
            }
            for (int i = tmp ; i < len ; i++){
                bookList.books[i] = bookList.books[i+1] ;
            }
            bookList.setBookSize(len);
            System.out.println("删除成功");
        }
    }
    public int search(BookList bookList) {
        System.out.println("请输入你要删除的图书名");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine() ;
        int len = bookList.getBookSize() ;
        for(int i = 0 ; i < len ; i ++){
            if(bookList.books[i].getName().equals(name)) {
                return i;
            }
        }
        return -1 ;
    }
}